package dev.tools;

import java.util.Arrays;
import java.util.List;

/**
 * The {@code IndexAnalysisTool} will analyze the .index file of DICT format.
 * 
 * This class analyzes and returns the stats of the percentage of it's alphabet
 * word presence in the index file.
 * 
 * @author Genzer
 * 
 */
public class IndexAnalysisTool {
    
    public IndexAnalysisTool(String indexFilePath) {
        this.indexFilePath = indexFilePath;
    }
    
    public void analyze() {
        performAnalyzing();
        printResult();
    }
    
    private void performAnalyzing() {
        List<String> allLines = readContent();
        
        for (String line : allLines) {
            analyzeWord(line);
        }
    }
    
    private List<String> readContent() {
        //IndexFileLoader fileLoader = new IndexFileLoader(indexFilePath);
        //return fileLoader.load();
        return null;
    }
    
    private void analyzeWord(String word) {
        char firstChar = word.charAt(0);
        
        if (isAlphabet(firstChar))  {
            countWordBeginningWithAlphabet(firstChar);
        } else if (isDigit(firstChar)) {
            countWordBeginningWithNumbericChar(firstChar);
        } else {
            countWordBeginningWithNonalphabetChar();
        }
        
    }
    
    private static boolean isAlphabet(char character) {
        boolean isLetter = Character.isLetter(character);        
        char lowerCasedChar = toLowerCase(character);  
        boolean isAlphabet = (lowerCasedChar >= 97 && lowerCasedChar <= 122);        
        return isLetter && isAlphabet;
    }
    
    private static char toLowerCase(char character) {
        return Character.toLowerCase(character);
    }
    
    private static boolean isDigit(char digit) {
        return Character.isDigit(digit);
    }
    
    
    private void countWordBeginningWithAlphabet(char alphabetChar) {    
        char lowerCasedChar = toLowerCase(alphabetChar);
        byte pos = (byte)(lowerCasedChar - ALPHABET_UNMASK_VALUE);
        alphabetTable[pos]++;
    }
    
    private void countWordBeginningWithNumbericChar(char numbericChar) {
        byte pos = (byte) (numbericChar - NUMBERIC_CHAR_UNMASK_VALUE);
        numberTable[pos]++;
    }
    
    private void countWordBeginningWithNonalphabetChar() {
        nonAlphabetTable++;
    }
    
    private void printResult() {
        System.out.println(alphabetTable[0]);
    }
    
    private final int[] alphabetTable = new int[26];
    private final int[] numberTable = new int[10];
    private int nonAlphabetTable;
    
    private static final byte ALPHABET_UNMASK_VALUE = 97;
    private static final byte NUMBERIC_CHAR_UNMASK_VALUE = 48;
    final String indexFilePath;
}
